import axios from "axios"
import BaseConfig from "../utils/baseConfig.js"
import datalist from '../assets/book.json';
/*查询图书，不传参数时返回全部图书*/
class Gategory {
    title?: string;//书名
    responseData?:any=datalist.data;//书的详情
    // 构造函数
    constructor(title?: string) {
        this.title = title
        console.log('datalist',datalist)        
        //this.getResultData();//返回书的详情
    }
    getResultData(){
        let _this=this;
        let params = {
            where:{
                title:_this.title
            }
        }
        axios({
            method: 'get',
            url: BaseConfig.apiUrl+'/'+BaseConfig.apiList.book[0].urls,
            headers: {
                'content-type': 'application/x-www-form-urlencoded',
                'x-apicloud-mcm-key': BaseConfig.apiKey
            },
            params: {
                filter: JSON.stringify(params),
            }
        }).then(function (response) {
                if (response.status == 200) {
                    //console.log(response.data);
                    _this.responseData=response.data
                    if(response.data.length>0){
                        //当有内容时再追加
                        _this.addMenu(response.data);
                    }               
                }
        }).catch(function (error) {
            console.log(error);
            if(_this.responseData.length>0){
                //显示默认数据库
                _this.addMenu(_this.responseData);
            }
        });
        return _this.responseData
    }
    addMenu(items:Array<any>){//将内容追加至左侧菜单
        var _this=this;
        let ulBlock=document.getElementById("ul_list") as HTMLElement;        
        let _html='';
        for(let i=0;i<items.length;i++){
            _html+="<li>"
            _html+="<div class='item_image'>"
            _html+="<a href=\"./reader.html?mediaId="+items[i].mediaId+"\">"
            _html+="<img src="+items[i].mediaPic+">"
            _html+="</a>"
            _html+="</div>"
            _html+="<div class='item_title'>"
            _html+="<a href=\"./reader.html?mediaId="+items[i].mediaId+"\">"
            _html+=items[i].title
            _html+="</a>"
            _html+="</div>"
            _html+="<p>"+items[i].description+"</p>"
            _html+="</li>"
        }
        ulBlock.innerHTML=_html;     
        
    }
}
export default Gategory